package com.maolong.api.doc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.maolong.util.JsonObjUtil;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.List;

/**
 * 封装增删改的接口
 */
public class EsDocMangeUtil {
    private static final String HOST = "localhost";
    private static final int PORT = 9200;
    private static final String SCHEME = "http";
    private static RestHighLevelClient client;
    private static ObjectMapper objectMapper=new ObjectMapper();

    static {
        client=new RestHighLevelClient(
                RestClient.builder(new HttpHost(HOST,PORT,SCHEME)));
    }

    /**
     * 进行文档的添加
     * @param indexName
     * @param obj
     * @param id
     * @return
     */

    public static boolean addDoc(String indexName,Object obj,String id){

        String json = JsonObjUtil.toJson(obj);
        IndexRequest request=new IndexRequest(indexName).id(id).source(json, XContentType.JSON);
        IndexResponse index = null;
        try {
            index = client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return index.getResult().equals("CREATED");
    }

    /**
     * 进行文档的修改
     * @param indexName
     * @param obj
     * @param id
     * @return
     */
    public static boolean updateDoc(String indexName,Object obj,String id){
        String json = JsonObjUtil.toJson(obj);
        UpdateRequest updateRequest=new UpdateRequest();
        updateRequest.index(indexName).id(id).doc(json,XContentType.JSON);
        UpdateResponse update = null;
        try {
            update = client.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return update.getResult().equals("UPDATED");
    }

    /**
     * 删除单条文档
     * @param indexName
     * @param id
     * @return
     */
    public static boolean delDoc(String indexName,String id){
        DeleteRequest deleteRequest=new DeleteRequest(indexName,id);
        DeleteResponse delete = null;
        try {
            delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return delete.getResult().equals("DELETED");
    }

    /**
     * 批量添加操作
     */
    public static <T> boolean addBatch(String indexName, List<T> list){
        BulkRequest bulkRequest=new BulkRequest();
        for (T obj : list) {
            bulkRequest.add(new IndexRequest(indexName).source(JsonObjUtil.toJson(obj), XContentType.JSON));
        }
        BulkResponse bulk = null;
        try {
            bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bulk.status().getStatus() == 200;
    }

    /**
     * 批量删除
     * @param indexName
     * @param list
     * @param id
     * @return
     */
    public static boolean deleteBatch(String indexName, List<Object> list, String id){
        return true;
    }

}
